package com.whf.Controller;

import com.whf.dto.OrderDTO;
import com.whf.enums.ResultEnum;
import com.whf.exception.SellException;
import com.whf.service.OrderService;
import com.whf.service.PushMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/7/17 0017.
 */


@Controller
@RequestMapping("/seller/order")
@Slf4j
public class SellerOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private PushMessageService pushMessageService;

    //显示订单列表
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                             @RequestParam(value = "size",defaultValue = "10") Integer size,
                             Map<String,Object> map){


        PageRequest pageRequest = new PageRequest(page-1, size);
        Page<OrderDTO> orderDTOPage = orderService.findAllList(pageRequest);
        map.put("orderDTOPage",orderDTOPage);
        map.put("currentPage",page);
        return new ModelAndView("/order/list",map);

    }
    //取消订单
    @GetMapping("/cancel")
    public ModelAndView cancel(@RequestParam("orderId") String orderId,
                               Map<String,Object> map){

        try{
            //根据orderid查询orderdto
            OrderDTO orderDTO = orderService.findOne(orderId);
            //取消订单
            OrderDTO cancel = orderService.cancel(orderDTO);
        }catch(SellException e){
            log.error("[卖家端取消订单] 取消订单失败");
            //提示信息
            map.put("msg",ResultEnum.ORDER_NOT_EXIST);
            //跳转的url
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error", map);
        }

        //提示信息
        map.put("msg",ResultEnum.SUCCESS_CANCEL);
        //跳转的url
        map.put("url","/sell/seller/order/list");
        return new ModelAndView("common/success", map);
    }

    //订单详情
    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam("orderId") String orderId,
                               Map<String,Object> map){

        try {
            //查询商品详情
            OrderDTO orderDTO = orderService.findOne(orderId);

            map.put("orderDTO",orderDTO);
            return new ModelAndView("order/detail",map);
        } catch (SellException e) {
            log.error("[卖家端查询订单详情] 查询详情出错,err={}",e.getMessage());
            map.put("msg",ResultEnum.FAIL_DETAIL);
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }


    }
    //完结订单
    @GetMapping("/finish")
    public ModelAndView finish(@RequestParam("orderId") String orderId,
                               Map<String,Object> map){
        try {
            OrderDTO one = orderService.findOne(orderId);
            if(one==null){
                log.error("[卖家端完结订单] 该订单不存在,orderId={}",orderId);
                throw new SellException(ResultEnum.ORDER_NOT_EXIST);
            }
            OrderDTO finish = orderService.finish(one);
            map.put("msg",ResultEnum.SUCCESS_FINISH);
            map.put("url","/sell/seller/order/list");
            //向买家发送消息模板
            pushMessageService.pushOrderStatus(finish);
            return new ModelAndView("common/success",map);
        } catch (SellException e) {
            log.error("[卖家端完结订单] 完结订单出错,err={}",e.getMessage());
            map.put("msg",ResultEnum.FAIL_FINISH);
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }

    }

}
